<?php
/**
 * 平台收货国家 (映射国家)（基础）
 * (model)
 */

namespace App\Models\Basics;

use Illuminate\Database\Eloquent\Model;
class SysPlatCountry extends Model{

    protected $table = 'b_sys_plat_country';

    public $timestamps = false;

    protected $guarded = [];

    /**
     * 关联国家
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function country()
    {
        return $this->belongsTo(SysCountry::class, 'country_id');
    }

    public static function options(){
        return self::query()->pluck('country_code','id');
    }

    /**
     * 获取平台发货国家id
     * @param $countryCode
     * @return int|mixed
     */
    public static function getCountryId($countryCode){
        if($countryCode == 'GB') $countryCode = 'UK';
        $countryId = self::query()->where('country_code', $countryCode)->value('country_id');
        if(!$countryId){
            $countryId = SysCountry::getIdByCountry('PUBLIC');
        }
        return $countryId;
    }

    /**
     * 获取发货站点
     * @param $countryId
     * @return \Illuminate\Support\Collection|string
     */
    public static function getAreaIdByCountryId($countryId){
        return SysICountry::getAreaIdByCountryId($countryId);
    }
}
